<template>
  <div class="app-container">
    <el-card class="box-card" shadow="false">
      <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="80px">
      </el-form>

      <el-row :gutter="10" class="mb8">
        <right-toolbar :show-search.sync="showSearch" @queryTable="getList"/>
      </el-row>

      <el-table v-loading="loading" :data="dataList" border>
        <el-table-column label="ID" prop="id" width="120px"/>
        <el-table-column label="培训计划名称" prop="planName"/>
        <el-table-column label="起止时间">
          <template slot-scope="scope">
            <div>
              起始：{{ scope.row.startTime }}
            </div>
            <div>
              结束：{{ scope.row.endTime }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="讲师" prop="offlineTrainLecturer"/>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <dict-tag size="mini" style="margin-right: 5px" :options="dict.type.train_plan_status"
                      :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column label="培训状态">
          <template slot-scope="scope">
            <dict-tag size="mini" style="margin-right: 5px" :options="dict.type.train_plan_offline_status"
                      :value="scope.row.trainStatus"/>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createTime"/>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              icon="el-icon-info"
              size="mini"
              type="text"
              @click="handleInfo(scope.row.id)">详情
            </el-button>
            <el-button
              :disabled="scope.row.status !== '1' || scope.row.trainStatus === '2'"
              icon="el-icon-success"
              size="mini"
              type="text"
              @click="handleSuccess(scope.row)">完成培训
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :limit.sync="queryParams.pageSize"
        :page.sync="queryParams.pageNum"
        :total="total"
        @pagination="getList"
      />

    </el-card>

    <el-dialog :visible.sync="modal" title="完成培训" width="35%">
      <el-form :data="form" label-position="top">
        <el-form-item label="* 培训人员">
          <el-transfer
            :props="{key: 'userId', label: 'nickName'}"
            :titles="['未培训人员', '已培训人员']"
            v-model="checkedUserIds"
            :data="data.users">
          </el-transfer>
        </el-form-item>
        <el-form-item label="备注">
          <el-input type="textarea" placeholder="请输入备注" v-model="form.remark"/>
        </el-form-item>
        <el-form-item label="* 附件">
          <fileUpload @input="getFiles" :limit="20" :file-size="20"
                      :file-type="['jpg', 'png', 'xls', 'xlsx', 'ppt', 'pptx', 'doc', 'docx', 'mp4', 'mp3']"/>
        </el-form-item>
      </el-form>

      <div style="text-align: right;">
        <el-button @click="modal = false">取 消</el-button>
        <el-button type="primary" @click="submit" :loading="submitLoading">{{
            submitLoading ? '提交中 ...' : '确 定'
          }}
        </el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="infoModal" title="培训详情">
      <el-descriptions title="培训计划信息" column="1">
        <el-descriptions-item label="计划名称">{{ info.planName }}</el-descriptions-item>
        <el-descriptions-item label="起止时间">{{ info.startTime }} 至 {{ info.endTime }}</el-descriptions-item>
        <el-descriptions-item label="负责人">{{ info.offlineTrainLecturer }}</el-descriptions-item>
        <el-descriptions-item label="考官">{{ info.offlineTrainLecturer }}</el-descriptions-item>
        <el-descriptions-item label="地点">{{ info.offlineTrainLocation }}</el-descriptions-item>
        <el-descriptions-item label="状态">
          <dict-tag size="mini" style="margin-right: 5px" :options="dict.type.train_plan_status"
                    :value="info.status"/>
        </el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ info.createTime }}</el-descriptions-item>
      </el-descriptions>
      <el-descriptions title="培训信息" column="1">
        <el-descriptions-item label="培训状态">
          <dict-tag size="mini" style="margin-right: 5px" :options="dict.type.train_plan_offline_status"
                    :value="info.trainInfo.status"/>
        </el-descriptions-item>
        <el-descriptions-item v-if="info.trainInfo.status === '2'" label="操作时间">{{ info.trainInfo.optTime }}</el-descriptions-item>
        <el-descriptions-item v-if="info.trainInfo.status === '2'" label="备注">{{ info.trainInfo.remark }}</el-descriptions-item>
        <el-descriptions-item v-if="info.trainInfo.status === '2'" label="附件">
          <div style="display: flex; flex-direction: column">
            <div v-for="(f,i) in info.trainInfo.fileList" :key="i"
                 style="width: 500px; height: 40px; border: 1px solid #d8c0c0; border-radius: 5px; display: flex; padding: 0 10px; align-items: center; margin-bottom: 10px">
              <el-link :href="f.url" target="_blank">{{ f.originalName }}</el-link>
            </div>
          </div>
        </el-descriptions-item>
        <el-descriptions-item v-if="info.trainInfo.status === '2'" label="培训合格">
          <div style="display: flex">
            <div v-for="(u, i) in info.trainInfo.completeUsers" :key="i"
                 style="display: flex; align-items: center; margin: 0 5px; border: 1px solid grey; padding:5px 10px; border-radius: 55px">
              <el-avatar :src="u.avatar" size="small" @error="true">{{ u.nickName[0] }}</el-avatar>
              <span style="margin-left: 5px">{{ u.nickName }}</span>
            </div>
          </div>
        </el-descriptions-item>
        <el-descriptions-item v-if="info.trainInfo.status === '2'" label="培训不合格">
          <div style="display: flex">
            <div v-for="(u, i) in info.trainInfo.incompleteUsers" :key="i"
                 style="display: flex; align-items: center; margin: 0 5px; border: 1px solid grey; padding:5px 10px; border-radius: 55px">
              <el-avatar :src="u.avatar" size="small" @error="true">{{ u.nickName[0] }}</el-avatar>
              <span style="margin-left: 5px">{{ u.nickName }}</span>
            </div>
          </div>
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import {finishOfflineTrain, getOfflineTrainInfo, getOfflineTrainPlans} from "@/api/edu/trainPlan";
import dict from "@/utils/dict";
import fileUpload from '@/components/FileUpload/index.vue'

export default {
  name: 'offlineTrain',
  dicts: ['train_plan_status', 'train_plan_offline_status'],
  components: {fileUpload},
  data() {
    return {
      loading: false,
      submitLoading: false,
      showSearch: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      dataList: [],
      data: {},
      infoModal: false,
      modal: false,
      checkedUserIds: [],
      form: {},
      info: {
        trainInfo: {}
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    dict,
    getList() {
      getOfflineTrainPlans(this.queryParams).then(res => {
        this.dataList = res.data.list
        this.total = res.data.total
      })
    },
    handleInfo(id) {
      getOfflineTrainInfo(id).then(res => {
        this.info = res.data
        this.infoModal = true
      })
    },
    handleSuccess(data) {
      this.data = data
      this.modal = true
    },
    getFiles(files) {
      this.form.files = files
    },
    submit() {
      this.submitLoading = true
      this.form.trainPlanId = this.data.id
      this.form.completeUserIds = this.checkedUserIds.toString()
      finishOfflineTrain(this.form).then(res => {
        this.submitLoading = false
        this.modal = false
        this.getList()
      })
    }
  }
}
</script>

<style scoped>

</style>
